package kulstats;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Key;

public  class MilestoneController {
	private static final MilestoneController instance = new MilestoneController();
	private MilestoneManager 	milestoneManager;
	private UserManager         userManager;
	private ActivityManager     activityManager;
	private User 				currentUser;
	
	private MilestoneController() {

		milestoneManager = Registry.milestoneManager();
		userManager     = Registry.userManager();
		activityManager     = Registry.activityManager();
		

	}
	
	public static MilestoneController getInstance() {
		
		return instance;
	}
	
	public boolean saveMilestone(Milestone mst) {
    	currentUser = userManager.getCurrentUser();

		CalculatePoints calcPts = new CalculatePoints();
		
		
		int points = calcPts.calculateMilestonePoints(mst);
		mst.setPoints(points);
		Key userKey = currentUser.getKey();
		return milestoneManager.saveMilestone(mst,userKey);
	}

	public double getProgress(Milestone mst) {
    	currentUser = userManager.getCurrentUser();

		Key userKey =		currentUser.getKey();
		double progress = 	milestoneManager.getProgress(mst,userKey);
		
		return progress;
	}
	public Map<String, LinkedList<Double>> getUnfinishedMilestones(){
    	currentUser = userManager.getCurrentUser();

		Map<String, LinkedList<Double>> unfinishedMilestones = milestoneManager.getUnfinishedMilestones(currentUser);
		//Reward the user for his achieved milestones
		
		return unfinishedMilestones;
	}
	
	public LinkedList<Long> getProgressWhileTracking(String trackCourse) {
    	currentUser = userManager.getCurrentUser();

		Key userKey = currentUser.getKey();
		long progress_tot = 0;
		long progress_now = 0;
		Milestone milestone = null;
		Milestone mst = milestoneManager.getMilestoneByCourse(userKey,trackCourse);
		if(mst !=null) {
			int hours = mst.getHours();
			progress_tot = (long)hours*60*60*1000;
			milestone = mst;
			
			long time =0;
			
			Date StopDateMilestone = milestone.getStopDate();
			Date StartDateMilestone = milestone.getStartDate();
			List<Activity> listAct = activityManager.getActivities(userKey);
			Iterator<Activity> it = listAct.iterator();
			while (it.hasNext()) {
					Activity act = it.next();
					if (act instanceof getCourseable){
						Course currCourse =((getCourseable)act).getCourse();
						String currCourseString = currCourse.getCourseID();
							if(currCourseString.equals(trackCourse)) {
								Date stopAct = act.getStopDate();
								Date startAct = act.getStartDate();
								if(stopAct != null && stopAct.before(StopDateMilestone)) {
									if(startAct.after(StartDateMilestone)) {
										time+= stopAct.getTime() - startAct.getTime();
									}
								}
							}
					}
			}
			progress_now = time;
		}
		LinkedList<Long> toReturn =  new LinkedList<Long>();
		toReturn.add(progress_now);
		toReturn.add(progress_tot);
		return toReturn;
	}
}